package com.qst.system.controller;

import com.qst.common.core.controller.BaseController;
import com.qst.common.core.domain.AjaxResult;
import com.qst.common.core.page.TableDataInfo;
import com.qst.system.domain.vo.OverdueVo;
import com.qst.system.service.IOverdueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/overdue")
public class OverdueController extends BaseController {

    @Autowired
    private IOverdueService overdueService;

    @PreAuthorize("@ss.hasPermi('system:overdue:list')")
    @GetMapping("/list")
    public TableDataInfo list( OverdueVo overdueVo) {
        startPage();
        List<OverdueVo> overdueVos = overdueService.selectOverdueList(overdueVo);
        System.out.println("进入控制层===========" + overdueVos.toString());
        return getDataTable(overdueVos);
    }
    @PreAuthorize("@ss.hasPermi('system:overdue:edit')")
    @GetMapping("/{oveId}")
    public AjaxResult selectOverdueById(@PathVariable("oveId") Long id) {
        System.out.println("=============================");
        return AjaxResult.success(overdueService.selectOverdueById(id));
    }
    @PreAuthorize("@ss.hasPermi('system:overdue:add')")
    @PostMapping
    public int insertOverdue(@RequestBody OverdueVo overdue) {
        return overdueService.insertOverdue(overdue);
    }

    @PreAuthorize("@ss.hasPermi('system:overdue:edit')")
    @PutMapping
    public int updateOverdue(@RequestBody OverdueVo overdue) {
        return overdueService.updateOverdue(overdue);
    }
    @PreAuthorize ("@ss.hasPermi ('system:overdue:remove')")
    @DeleteMapping ("/{ids}")
    public AjaxResult deleteOverdueByIds (@PathVariable String ids) {
        try {

            if (!ids.contains (",")) {
                return toAjax (overdueService.deleteOverdueById (Long.parseLong (ids)));
            }

            String [] idStrs = ids.split (",");
            Long [] idsArr = new Long [idStrs.length];
            for (int i = 0; i < idStrs.length; i++) {
                idsArr [i] = Long.parseLong (idStrs [i].trim ());
            }
            return toAjax (overdueService.deleteOverdueByIds (idsArr));
        } catch (NumberFormatException e) {
            return AjaxResult.error ("删除失败，ID 格式不正确");
        } catch (Exception e) {
            return AjaxResult.error ("删除失败：" + e.getMessage ());
        }
    }
}
